<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:widget="http://www.netvibes.com/ns/">
	<head>
		<title>Google Calendar Agenda</title>
		<link rel="icon" href="http://www.google.com/calendar/images/favicon.ico" type="image/x-icon" />

    		<meta name="author" content="Matt Johnson" />
    		<meta name="description" content="A Google Calendar Agenda Netvibes UWA widget." />
    		<meta name="apiVersion" content="1.1" />
    		<meta name="inline" content="false" />
		<meta name="debugMode" content="false" />

    		<link rel="stylesheet" type="text/css" href="http://www.netvibes.com/themes/uwa/style.css" />
		<script type="text/javascript" src="http://www.netvibes.com/js/UWA/load.js.php?env=Standalone"></script>

    		<widget:preferences>
			<preference name="height" type="range" label="Widget Height" min="100" max="500" step="10" defaultValue="240" />
			<preference name="numdays" type="range" label="Look Ahead Days" min="1" max="30" step="1" defaultValue="7" />
			<preference name="cal1" type="text" label="Calendar 1" />
			<preference name="cal2" type="text" label="Calendar 2" />
			<preference name="cal3" type="text" label="Calendar 3" />
			<preference name="cal4" type="text" label="Calendar 4" />
			<preference name="cal5" type="text" label="Calendar 5" />
			<preference name="cal6" type="text" label="Calendar 6" />
			<preference name="cal7" type="text" label="Calendar 7" />
			<preference name="cal8" type="text" label="Calendar 8" />
			<preference name="cal9" type="text" label="Calendar 9" />
			<preference name="padding" type="boolean" label="Remove Widget Padding" defaultValue="false" />
		</widget:preferences>

    		<style type="text/css">
     		/* your CSS rules */
    		</style>

    		<script type="text/javascript">
			var GoogleCalendarAgenda = {}

			GoogleCalendarAgenda.load = function() {
				var myURL = widget.getValue('url');
				var myHeight = parseInt(widget.getValue('height'));
				var myDays = parseInt(widget.getValue('numdays'));
				var myDateRange = GoogleCalendarAgenda.parseDays(myDays);
				var myURL = GoogleCalendarAgenda.generateURL(myDateRange,myHeight);
				var myPadding = widget.getValue('padding');

				var contentHTML = "";	
				if (myURL == null) {
					contentHTML = "<p>You do not currently have any calendars specified.  Please click the edit link above to add a calendar to this gadget.<br/><br/>Each calendar entry box can be specified with a <a href=\"http://calendar.google.com\">Google Calendar</a> HTML address (See <a href=\"http://www.google.com/support/calendar/bin/answer.py?answer=37103\">here</a> for more help regarding finding your calendar's HTML address).</p>";
				} else {
					if (myPadding == 'true') widget.body.style.padding = "0";
					contentHTML = "<iframe src=\""+myURL+"\" style=\"width:100%;border-width:0;\" frameborder=\"0\" height=\""+myHeight+"px\"></iframe>";
				}
	
				widget.setBody(contentHTML); 
			}

			GoogleCalendarAgenda.parseDays = function(myDays) {
				var start = new Date();
				var end = new Date();
				end.setDate(start.getDate() + myDays);

				var startYear = start.getFullYear();
				var startMonth = 1 + start.getMonth();
				var startDay = start.getDate();

				var endYear = end.getFullYear();
				var endMonth = 1 + end.getMonth();
				var endDay = end.getDate();

				if (startMonth < 10) startMonth = "0" + startMonth;
				if (endMonth < 10) endMonth = "0" + endMonth;
				if (startDay < 10) startDay = "0" + startDay;
				if (endDay < 10) endDay = "0" + endDay;

				return "" + startYear + startMonth + startDay + "/" + endYear + endMonth + endDay;
			}

			GoogleCalendarAgenda.generateURL = function(myDateRange,myHeight) {
				var baseURL = "http://www.google.com/calendar/embed?";
				var startURL = "http://www.google.com/calendar/htmlembed?";
				var myURL = startURL;

				if (widget.getValue("cal1") != null && widget.getValue("cal1") != '') myURL = myURL + widget.getValue("cal1").replace(baseURL,'') + "&";
				if (widget.getValue("cal2") != null && widget.getValue("cal2") != '') myURL = myURL + widget.getValue("cal2").replace(baseURL,'') + "&";
				if (widget.getValue("cal3") != null && widget.getValue("cal3") != '') myURL = myURL + widget.getValue("cal3").replace(baseURL,'') + "&";
				if (widget.getValue("cal4") != null && widget.getValue("cal4") != '') myURL = myURL + widget.getValue("cal4").replace(baseURL,'') + "&";
				if (widget.getValue("cal5") != null && widget.getValue("cal5") != '') myURL = myURL + widget.getValue("cal5").replace(baseURL,'') + "&";
				if (widget.getValue("cal6") != null && widget.getValue("cal6") != '') myURL = myURL + widget.getValue("cal6").replace(baseURL,'') + "&";
				if (widget.getValue("cal7") != null && widget.getValue("cal7") != '') myURL = myURL + widget.getValue("cal7").replace(baseURL,'') + "&";
				if (widget.getValue("cal8") != null && widget.getValue("cal8") != '') myURL = myURL + widget.getValue("cal8").replace(baseURL,'') + "&";
				if (widget.getValue("cal9") != null && widget.getValue("cal9") != '') myURL = myURL + widget.getValue("cal9").replace(baseURL,'') + "&";

				if (myURL == startURL) {
					myURL = null;
				} else {
					myURL = myURL + "chrome=NONE&mode=AGENDA&height=" + myHeight + "&dates=" + myDateRange;
				}

				return myURL;
			}

			widget.onLoad = function() {
				GoogleCalendarAgenda.load();
			}

			widget.onRefresh = function() {
				GoogleCalendarAgenda.load();
			}

			widget.onResize = function() {
				//GoogleCalendarAgenda.load();
			}
		</script>
	</head>

	<body>
		Loading...
	</body>
</html>
